package com.lottery.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static String getFormatDate(Date date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static String getFormatDate(String format) {
		return getFormatDate(new Date(), format);
	}

	public static Date getDateFromString(String dateString, String formatString) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(formatString);

		return format.parse(dateString);
	}

	public static String getDateFromString(String dateString, String formatString, int delayMinute) {
		Date date = null;

		try {
			date = getDateFromString(dateString, formatString);
		} catch (ParseException e) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();

		if (date != null)
			calendar.setTime(date);

		calendar.add(Calendar.MINUTE, delayMinute);
		date = calendar.getTime();

		SimpleDateFormat format = new SimpleDateFormat(formatString);

		return format.format(date);
	}

	public static long dateToStamp(String s, String format) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = simpleDateFormat.parse(s);
		long ts = date.getTime();
		return ts;
	}

	public static String stampToDate(long lt) {
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(lt);
		res = simpleDateFormat.format(date);
		return res;
	}

	public static String stampToDate(long lt,String format) {
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = new Date(lt);
		res = simpleDateFormat.format(date);
		return res;
	}

	public static String timeStamp2Date(long seconds, String format) {
		if (format == null || format.isEmpty()) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(seconds * 1000));
	}

	public static long dateToStamp(String s) {
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		long ts = date.getTime();
		return ts;
	}

	public static void main(String[] args) {
		System.out.println(dateToStamp("20171201011214"));
	}
}
